﻿using HCQ2_Common;
using HCQ2_Common.Constant;
using HCQ2_Model.Data;
using HCQ2_Model.ParamsModel;
using HCQ2_Model.Pay;
using HCQ2_Model.ViewModel;
using HCQ2_Model.ViewModel.VR;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    /// 订单管理
    /// </summary>
    public class OrderController:BaseLogic
    {
        
        public ActionResult Index(int id)
        {
            //0：未付款，1：已撤销，2：交易成功，3：交易关闭
            if (id == 0) {
                ViewBag.t = "未付款订单";
            }
            if (id == 1)
            {
                ViewBag.t = "已撤销订单";
            }
            if (id == 2)
            {
                ViewBag.t = "交易成功订单";
            }
            if (id == 3)
            {
                ViewBag.t = "交易管理订单";
            }
            ViewBag.type = id;

            return View();
        }

        /// <summary>
        /// 全部订单
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderSystem()
        {
            ViewBag.UserId = operateContext.Usr.user_guid;
            ViewBag.ApiUrl = ConfigurationHelper.AppSetting(SystemConstant.APIURL_ROOT_NAME);
            ViewBag.Title = "所有订单";
            ViewBag.OrderType = 0;
            ViewBag.ProType = operateContext.bllSession.T_ItemCodeMenum.ListByItemId(12);
            return View("IndexOrder");
        }

        /// <summary>
        /// 买家订单
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderBuyer()
        {
            ViewBag.UserId = operateContext.Usr.user_guid;
            ViewBag.ApiUrl = ConfigurationHelper.AppSetting(SystemConstant.APIURL_ROOT_NAME);
            ViewBag.Title = "已买到的商品";
            ViewBag.OrderType = 1;
            ViewBag.ProType = operateContext.bllSession.T_ItemCodeMenum.ListByItemId(12);
            return View("IndexOrder");
        }
        /// <summary>
        /// 卖家订单
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderSeller()
        {
            ViewBag.UserId = operateContext.Usr.user_guid;
            ViewBag.ApiUrl = ConfigurationHelper.AppSetting(SystemConstant.APIURL_ROOT_NAME);
            ViewBag.Title = "已卖出的商品";
            ViewBag.OrderType = 2;
            ViewBag.ProType = operateContext.bllSession.T_ItemCodeMenum.ListByItemId(12);
            return View("IndexOrder");
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="listParams"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult OrderList(GetPcOrderListParams listParams)
        {
            listParams.userid = operateContext.Usr.user_guid;
            OperationResult<TableModel> operationResult = operateContext.bllSession.N_Order.ListOrderList(listParams);

            if (operationResult.Success) {
                return operateContext.ok(operationResult.Data);
            }

            return operateContext.error(operationResult.Message);
        }

        /// <summary>
        /// 查询订单商品详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detailed(string id)
        {
            OrderDetailed orderDetailed = operateContext.bllSession.T_VROrder.GetOrderDetailed(id);

            ViewBag.detailed = orderDetailed;
            return View();
        }


        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="orderListParams">查询参数</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetOrderList(OrderListParams orderListParams)
        {
            TableModel orderList = operateContext.bllSession.T_VROrder.GetOrderList(orderListParams);
            return operateContext.ok(orderList);
        }

        /// <summary>
        /// 付款
        /// </summary>
        /// <param name="orderCode">订单编码</param>
        /// <param name="pramoney">实际支付金额</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Payment(string orderCode, decimal pramoney)
        {
            operateContext.bllSession.T_VROrder.Payment(orderCode, pramoney);
            return operateContext.ok("付款成功");
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="orderCode">订单编码</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Revoke(string orderCode)
        {
            operateContext.bllSession.T_VROrder.Revoke(orderCode);
            return operateContext.ok("取消订单成功");
        }

        /// <summary>
        /// 导出订单列表
        /// </summary>
        /// <param name="listParams"></param>
        [HttpPost]
        public JsonResult exp(GetPcOrderListParams listParams)
        {
            string fileName = "导出订单列表" + DateTime.Now.ToString("yyyy-MM-dd-hhmmss");

            string filePath = "/uploadFile/OrderExp/" + Guid.NewGuid().ToString() + "/";

            string expFilePath = Server.MapPath(Request.ApplicationPath + filePath);

            Directory.CreateDirectory(expFilePath);
            

            DataTable data = operateContext.bllSession.N_Order.ListOrderListExpData(listParams);
            NpoiHelper.DataTableToExcel03(data, "sheet1", expFilePath+ fileName);

            return operateContext.ok(filePath+ fileName+".xls");
        }
    }
}
